<?php

use Elite\Infrastructure\Server\Listener\Listener;

/**
 * 执行这个事件时Worker已经启动了，所以可以使用自动加载以及框架提供的功能
 */
class OnOpenListener extends Listener {

    public function event() : string {
        return 'open';
    }

    public function callback() {
        $server = func_get_arg(0);
        $request = func_get_arg(1);

        if (!isset($request->get['tk'])) {
            $server->push($request->fd, 'Invalid param.');
            $server->close($request->fd);
            return;
        }

        if (is_numeric($request->get['tk'])) {
            $server->push($request->fd, 'Invalid param.');
            $server->close($request->fd);
            return;
        }
        // TODO 验证tk

        $fdHolder = $this->swoole->getFdHolder();
        $fdHolder->put($request->get['tk'], $request->fd);
        $fdHolder->put($request->fd, $request->get['tk']);
        print_r($request->fd, $request->get['tk']);
    }

}